﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Smo.Agent;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management;
namespace DAO
{
    public class RESTORE
    {
        public static bool khoiphuc()
        {
            
            string databasename = "QUAN_LY_THU_VIEN";
            string backupname = "TESTBAKUP";
            try
            {
                ServerConnection serverconn = new ServerConnection();
                serverconn.LoginSecure = true;
                serverconn.ServerInstance = @"(local)\SQLEXPRESS";

                Server sqlserver = new Server(serverconn);
                sqlserver.KillAllProcesses(databasename);
                Restore restore = new Restore();

                restore.Action = RestoreActionType.Database;
                bool co = false;
                foreach (BackupDevice device in sqlserver.BackupDevices)
                {
                    if (device.Name == backupname)
                    {
                        co = true;
                        break;
                    }
                }
                if (co == true)
                {
                    restore.Devices.AddDevice(backupname, DeviceType.LogicalDevice);
                    restore.Database = databasename;
                    restore.NoRecovery = true;
                    restore.SqlRestore(sqlserver);
                    return true;
                }
                else return false;
            }
            catch 
            {
                return false;
            }
        }
    }
}
